package com.appspot.alcappmobile;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class hallofshame extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hallofshame);
        RestClient client = new RestClient("http://alcappmobile.appspot.com/listenforrestclient");
        //Rest client retrieves all of the HTML and stores it in 2 array lists, "params" and "headers"          
        //client.AddParam("message", "Nav");
        
        try
        {
            client.Execute(RequestMethod.GET);
        }
        catch (Exception e)
        {
            return;
        }
        
        String response = client.getResponse();

      //parsing with userName and response
        String r = response;
		  String temp2 = r;
		  int i = -1;
		  int k = 0;
		  int m = 0;
		  int n = 0;
		  double[] bac;
		  bac = new double[1024];
		  String[] name;
		  name = new String[1024];
		  int[] drinks;
		  drinks = new int[1024];
		  i = r.indexOf("</td></tr></table>");
        while(i != -1)
        {
				//grab a single users info
        		
            	
            	temp2 = r.substring(i+2);//remove the current users data
            	r = r.substring(0,i);//now we have one users data
            	n = r.indexOf("<tr><th>");//find beginning of name
            	k = r.indexOf("</th><th>");//find end of name
            	name[m] = r.substring(n+8,k);//set name[m] to persons name		
  
	            String w = r;
	            String b = r;
	            String s = r;
	              
	            int temp;
	            int wNum = 0, bNum = 0, sNum = 0;
	              
				while(b.indexOf("beer") != -1)
				{
	             	bNum++;
	              	temp = b.indexOf("beer");
	              	b = b.substring(temp+4);
				}
	              
	             
	              while(w.indexOf("wine") != -1)
	              {
	              	wNum++;
	              	temp = w.indexOf("wine");
	              	w = w.substring(temp+4);
	              }
	              
	              
	              while(s.indexOf("shot") != -1)
	              {
	 
	            	sNum++;
	              	temp = s.indexOf("shot");
	              	s = s.substring(temp+4);
	              }
	              
	              drinks[m] = (wNum+sNum+bNum);
	              double currentBAC = BAC(wNum,sNum,bNum,200,4);
	              //reduce BAC to 2 decimal places             
				  bac[m] = currentBAC;
				  bac[m+1] = -2;
				  m++;	
            	  r = temp2;
            	  i = r.indexOf("</td></tr></table>");
				
				
  	}    
		double[] top3;
		top3 = new double[3];
		top3[0] = -1;
		top3[1] = -1;
		top3[2] = -1;
		String[] top3Names;
		top3Names = new String[3];
		i = 0;
		int[] top3Drinks;
        top3Drinks = new int[3];
		while(bac[i] != -2)
		{
			if(bac[i] > top3[0])
			{
				//push prev first to second and prev second to thrid
				top3[2] = top3[1];
				top3[1] = top3[0];
				top3[0] = bac[i];
				top3Names[2] = top3Names[1];
				top3Names[1] = top3Names[0];
				top3Names[0] = name[i];
				top3Drinks[2] = top3Drinks[1];
				top3Drinks[1] = top3Drinks[0];
				top3Drinks[0] = drinks[i];
				i++;
			}
			else if(bac[i] > top3[1])
			{
				//push second to thrid
				top3[2] = top3[1];
				top3Names[2] = top3Names[1];
				top3Drinks[2] = top3Drinks[1];
				top3[1] = bac[i];
				top3Names[1] = name[i];
				top3Drinks[1] = drinks[i];
				i++;
			}
			else if(bac[i] > top3[2])
			{
				top3[2] = bac[i];
				top3Names[2] = name[i];
				top3Drinks[2] = drinks[i];
				i++;
			}						
		}
		
		String first = String.format("%.3g", top3[0]);
		String second = String.format("%.3g", top3[1]);
		String thrid = String.format("%.3g", top3[2]);  
		
		//output data to screen
		TextView textView = (TextView)findViewById(R.id.Name_g);
		textView.setText(top3Names[0]);
		TextView textView2 = (TextView)findViewById(R.id.Drinks_g);
		textView2.setText(Integer.toString(top3Drinks[0]));
		TextView textView3 = (TextView)findViewById(R.id.BACgold);
		textView3.setText(first);
		TextView textView4 = (TextView)findViewById(R.id.Name_s);
		textView4.setText(name[0]);
		TextView textView5 = (TextView)findViewById(R.id.Drinks_s);
		textView5.setText(Integer.toString(top3Drinks[1]));
		TextView textView6 = (TextView)findViewById(R.id.BACsilver);
		textView6.setText(second);
		TextView textView7 = (TextView)findViewById(R.id.Name_b);
		textView7.setText(top3Names[2]);
		TextView textView8 = (TextView)findViewById(R.id.Drinks_b);
		textView8.setText(Integer.toString(top3Drinks[2]));
		TextView textView9 = (TextView)findViewById(R.id.BACbronze);
		textView9.setText(thrid);
		
        //back button code
        Button next = (Button) findViewById(R.id.hosBack);
        next.setOnClickListener
        (
        		new View.OnClickListener() 
        		{
        			public void onClick(View view) 
        			{
        				Intent myIntent = new Intent(hallofshame.this,alcmobile.class);
        				hallofshame.this.startActivity(myIntent);
        				
        			}

        		}
        );
    }
    
    public double BAC(int winenum, int shotnum, int beernum, int weight, int hours)
    {   
    // %BAC = (A x 5.14/W x g) - .015 x H 
    // A = Actual # of ozs of Alcohol in Drink 
    // W = Weight, g = .73 for men, r = .66 for women 
    // H = Height 
    int beeramt = 12;
    int wineamt = 5;
    
    // calculate the total ounces of pure alcohol
    double alcohol = (beeramt*beernum*.07) + (winenum*wineamt*0.12) + (shotnum*0.4);
    // float BAC=0, A=0, W=0, g=0, h=0;
    // There should be code here to get information from user to input into variables 
    
    
    // End Input from user 
    
    return (((alcohol*5.14) / (weight*0.73)) - (0.015*hours));

    }    
}